package com.example.demo2.service;


import com.example.demo2.entity.Student;
import com.example.demo2.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Optional;

@Service
public class StudentService {
    @Autowired
    StudentRepository studentRepository;

    public Student findById(Integer id) {
       Optional<Student> optionalStudent= studentRepository.findById(id);
       return optionalStudent.orElse(null);

    }
    public Student update(Student student){
        Optional<Student> optionalStudent = studentRepository.findById(student.getId());
        if (optionalStudent.isPresent()){
            studentRepository.save(student);
            return student;
        }
        else {
            return null;
        }
    }

    public Student insert(Student student){
        studentRepository.save(student);
        return student;
    }

    public void deleteById(Integer id){
        studentRepository.deleteById(id);
    }

}

